<template>
  <div class="divBox">
    <el-card class="box-card">
      <upload-index
        @openAddDialog="openAddDialog"
        @openEditDialog="openEditDialog"
        @getCategoryList="getCategoryList"
        @updataDelete="updataDelete"
        :treeData2="tableData.data"
      ></upload-index>
    </el-card>
    <!-- 添加素材对话框 -->
    <el-dialog
      title="添加素材分类"
      :visible.sync="addDialogVisible"
      @close="addDialogClosed"
      width="50%"
    >
      <el-form ref="form" :model="addForm" label-width="80px">
        <!-- 父级分类级联选择器 -->
        <el-form-item label="父级分类 :">
          <el-cascader
            v-model="selectedCateKEys"
            placeholder="请选择父级分类"
            clearable
            :options="tableData.data"
            :props="tableDatada"
            expand-trigger="hover"
            @change="handleChange"
          ></el-cascader>
        </el-form-item>
        <el-form-item label="分类名称">
          <el-input v-model="addForm.name"></el-input>
        </el-form-item>
        <el-form-item label="分类路径">
          <el-input v-model="addForm.path"></el-input>
        </el-form-item>
        <!-- 排序 -->
        <el-form-item label="排序 :">
          <el-input-number v-model="addForm.sort" :step="1"></el-input-number>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitAdd">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 编辑素材对话框 -->
    <el-dialog
      title="添加素材分类"
      :visible.sync="editDialogVisible"
      width="50%"
    >
      <el-form ref="form" :model="editForm" label-width="80px">
        <!-- 父级分类级联选择器 -->
        <el-form-item label="父级分类 :">
          <el-cascader
            v-model="selectedCateKEys"
            placeholder="请选择父级分类"
            clearable
            :options="tableData.data"
            :props="tableDatada"
            expand-trigger="hover"
            @change="handleChange"
          ></el-cascader>
        </el-form-item>
        <el-form-item label="分类名称">
          <el-input v-model="editForm.name"></el-input>
        </el-form-item>
        <el-form-item label="分类路径">
          <el-input v-model="editForm.path"></el-input>
        </el-form-item>
        <!-- 排序 -->
        <el-form-item label="排序 :">
          <el-input-number v-model="editForm.sort" :step="1"></el-input-number>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitEdit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import UploadIndex from "@/components/uploadPicture/index.vue";
import {
  attachmentCategoryList,
  attachmentCategoryCreate,
  attachmentCategoryInfo,
  attachmentCategoryUpdate,
} from "@/api/system.js";
export default {
  name: "Picture",
  components: { UploadIndex },
  data() {
    return {
      addDialogVisible: false,
      editDialogVisible: false,
      addForm: {},
      editForm: {},
      // 级联选择框双向绑定的数组
      selectedCateKEys: [],
      tableDatada: {
        value: "id",
        label: "name",
        children: "child",
        checkStrictly: true,
      },
      tableData: {
        data: [],
        total: 0,
      },
    };
  },
  created() {
    this.getCategoryList();
  },
  methods: {
    // 获取父级id
    getCategoryList() {
      attachmentCategoryList().then((res) => {
        console.log(res, "000000000");
        this.tableData.data = res.data;
      });
    },
    updataDelete(){
      this.getCategoryList();
    },
    // 级联选择器改变时触发该函数
    handleChange() {
      if (this.selectedCateKEys) {
        this.selectedCateKEys = this.selectedCateKEys[
          this.selectedCateKEys.length - 1
        ];
      } else {
        this.selectedCateKEys = 0;
      }
      console.log(this.selectedCateKEys);
    },
    // 打开添加对话框
    openAddDialog(id) {
      this.addDialogVisible = true;
      this.selectedCateKEys = id;
    },
    // 打开编辑对话框
    openEditDialog(id) {
      attachmentCategoryInfo(id)
        .then((res) => {
          this.editForm = res.data;
          this.selectedCateKEys = id;
          this.editDialogVisible = true;
        })
        .catch((res) => {
          console.log(id);
          console.log(res);
          this.$message.error("获取失败");
        });
    },
    // 编辑按钮
    submitEdit() {
      attachmentCategoryUpdate(this.selectedCateKEys, this.editForm)
        .then((res) => {
          this.editDialogVisible = false;
          this.$message.success("修改成功");
          this.getCategoryList();
          console.log(res);
        })
        .catch((res) => {
          this.$message.error("修改失败");
          console.log(res,"55555");
        });
    },
    // 添加按钮
    submitAdd() {
      if (this.selectedCateKEys.length !== 0) {
        this.addForm.pid = this.selectedCateKEys;
      } else {
        this.addForm.pid = 0;
      }
      console.log(this.addForm);
      attachmentCategoryCreate(this.addForm)
        .then((res) => {
          this.getCategoryList();
          console.log(res, "456465465");
          this.addDialogVisible = false;
          this.$message.success("操作成功");
        })
        .catch((res) => {
          console.log(res);
          this.$message.error("添加失败");
        });
    },
    addDialogClosed(){
      this.$refs.form.resetFields();
      this.addForm = {}
    }
  },
};
</script>

<style scoped>
</style>
